﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation.ConfigSetting
{
    public partial class AccessFilePassSet : Form
    {
        public AccessFilePassSet()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtOldPassWord.Text.Trim()))
            {
                MessageBox.Show("原密码不能为空！");
                txtOldPassWord.Focus();
                return;
            }
            //原始密码
            string password = CommonHelper.Decrypt(ConfigurationManager.AppSettings["AccessFilePass"].ToString(), "tp_ltscm");
            if (password != txtOldPassWord.Text.Trim())
            {
                MessageBox.Show("原密码不正确,请重新输入！");
                txtOldPassWord.Focus();
                return;
            }
            //判断原始密码是否正确
            if (string.IsNullOrEmpty(txtNewPassWord.Text.Trim()))
            {
                MessageBox.Show("新密码不能为空！");
                txtNewPassWord.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtConfirmPassWord.Text.Trim()))
            {
                MessageBox.Show("确认密码不能为空！");
                txtConfirmPassWord.Focus();
                return;
            }
            if (txtNewPassWord.Text.Trim() != txtConfirmPassWord.Text.Trim())
            {
                MessageBox.Show("新密码与确认密码不一致,请重新输入！");
                txtNewPassWord.Focus();
                return;
            }
            //修改配置文件的数据文件密码字段
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string accessFilePassStr = CommonHelper.Encrypt(txtNewPassWord.Text.Trim(),"tp_ltscm");
            cfa.AppSettings.Settings["AccessFilePass"].Value = accessFilePassStr;//设置config的值
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
